<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>会议室管理 - 会议室预订系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 1400px;
            margin: 0 auto;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }

        .container::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            transform: rotate(45deg);
            z-index: -1;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .header p {
            color: #666;
            font-size: 16px;
        }

        .search-form {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .form-title {
            font-size: 20px;
            margin-bottom: 20px;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
        }

        .form-group label {
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-group input, .form-group select {
            padding: 12px 15px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .form-group input:focus, .form-group select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-buttons {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }

        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .search-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .search-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 7px 15px rgba(102, 126, 234, 0.3);
        }

        .reset-btn {
            background: #f1f2f6;
            color: #666;
        }

        .reset-btn:hover {
            background: #dfe4ea;
        }

        .results-section {
            margin-top: 30px;
        }

        .section-title {
            font-size: 20px;
            margin-bottom: 20px;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .table-container {
            overflow-x: auto;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            background: #4a90e2;
            color: white;
            font-weight: 600;
        }

        tr:hover {
            background-color: #f8f9fa;
        }

        .no-results {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 18px;
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 25px;
            border-radius: 8px;
            color: white;
            font-weight: 500;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            z-index: 2000;
            display: none;
            animation: slideIn 0.3s ease;
        }

        .notification.success {
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
        }

        .notification.error {
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
        }

        @keyframes slideIn {
            from {
                transform: translateX(100%);
                opacity: 0;
            }
            to {
                transform: translateX(0);
                opacity: 1;
            }
        }

        /* 返回链接容器 */
        .back-link-container {
            margin-bottom: 20px;
            position: relative;
            z-index: 1;
        }

        /* 返回链接样式 */
        .back-link {
            display: inline-flex;
            align-items: center;
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            padding: 8px 16px;
            border-radius: 8px;
        }

        .back-link:hover {
            background: rgba(102, 126, 234, 0.1);
            text-decoration: none;
            transform: translateX(-3px);
        }

        /* 状态标签样式 */
        .status-badge {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: bold;
            text-align: center;
        }

        .status-free {
            background-color: #2ecc71;
            color: white;
        }

        .status-occupied {
            background-color: #e74c3c;
            color: white;
        }

        /* 会议室设施样式 */
        .facilities-list {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
        }

        .facility-tag {
            background-color: #e1f0fa;
            color: #667eea;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 12px;
        }

        /* 会议信息弹窗样式 */
        .meeting-info {
            margin-top: 5px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 5px;
            font-size: 13px;
        }

        .meeting-item {
            margin-bottom: 8px;
            padding-bottom: 8px;
            border-bottom: 1px dashed #ddd;
        }

        .meeting-item:last-child {
            margin-bottom: 0;
            padding-bottom: 0;
            border-bottom: none;
        }

        .meeting-topic {
            font-weight: bold;
            color: #333;
        }

        .meeting-time {
            color: #666;
            font-size: 12px;
        }

        @media (max-width: 768px) {
            .form-grid {
                grid-template-columns: 1fr;
            }
            
            th, td {
                padding: 10px 8px;
                font-size: 14px;
            }
            
            .container {
                max-width: 100%;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="back-link-container">
            <a href="/api/major.html" class="back-link">
                <i class="fas fa-arrow-left"></i> 返回主页
            </a>
        </div>
        
        <div class="header">
            <h1><i class="fas fa-door-open"></i> 会议室管理</h1>
            <p>查看会议室设备信息和状态</p>
        </div>
        
        <div class="search-form">
            <h2 class="form-title"><i class="fas fa-clock"></i> 时间段查询</h2>
            <form id="searchForm">
                <div class="form-grid">
                    <div class="form-group">
                        <label for="startTime">开始时间</label>
                        <input type="datetime-local" id="startTime" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="endTime">结束时间</label>
                        <input type="datetime-local" id="endTime" required>
                    </div>
                </div>
                
                <div class="form-buttons">
                    <button type="button" class="btn reset-btn" onclick="resetForm()">
                        <i class="fas fa-redo"></i> 重置
                    </button>
                    <button type="submit" class="btn search-btn">
                        <i class="fas fa-search"></i> 查询
                    </button>
                </div>
            </form>
        </div>
        
        <div class="results-section">
            <h2 class="section-title"><i class="fas fa-list"></i> 查询结果</h2>
            <div class="table-container">
                <table id="resultsTable">
                    <thead>
                        <tr>
                            <th>会议室ID</th>
                            <th>会议室名称</th>
                            <th>设备设施</th>
                            <th>状态</th>
                            <th>当前会议</th>
                        </tr>
                    </thead>
                    <tbody id="resultsTableBody">
                        <tr>
                            <td colspan="5" class="no-results">请输入查询条件并点击查询按钮</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <!-- 通知 -->
    <div id="notification" class="notification"></div>
    
    <script>
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 设置默认时间（当前时间到一小时后）
            const now = new Date();
            const oneHourLater = new Date(now.getTime() + 60 * 60 * 1000);
            
            // 格式化时间用于datetime-local输入框
            const formatDateTime = (date) => {
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                const hours = String(date.getHours()).padStart(2, '0');
                const minutes = String(date.getMinutes()).padStart(2, '0');
                return `${year}-${month}-${day}T${hours}:${minutes}`;
            };
            
            document.getElementById('startTime').value = formatDateTime(now);
            document.getElementById('endTime').value = formatDateTime(oneHourLater);
            
            // 表单提交事件
            document.getElementById('searchForm').addEventListener('submit', function(e) {
                e.preventDefault();
                searchRoomStatus();
            });
        });
        
        // 查询会议室状态
        function searchRoomStatus() {
            const startTime = document.getElementById('startTime').value;
            const endTime = document.getElementById('endTime').value;
            
            // 验证输入
            if (!startTime || !endTime) {
                showNotification('请填写完整的开始时间和结束时间', 'error');
                return;
            }
            
            // 格式化时间参数
            const start = new Date(startTime);
            const end = new Date(endTime);
            
            // 验证时间范围
            if (end <= start) {
                showNotification('结束时间必须晚于开始时间', 'error');
                return;
            }
            
            // 格式化为API需要的格式
            const formatTime = (date) => {
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                const hours = String(date.getHours()).padStart(2, '0');
                const minutes = String(date.getMinutes()).padStart(2, '0');
                return `${year}-${month}-${day} ${hours}:${minutes}`;
            };
            
            const params = new URLSearchParams({
                startTime: formatTime(start),
                endTime: formatTime(end)
            });
            
            // 发送查询请求
            fetch(`/api/meetings/rooms/status?${params.toString()}`)
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        displayResults(data.data);
                    } else {
                        displayResults([]);
                        showNotification(data.msg || '查询失败', 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showNotification('查询时发生错误', 'error');
                    displayResults([]);
                });
        }
        
        // 显示查询结果
        function displayResults(rooms) {
            const tbody = document.getElementById('resultsTableBody');
            tbody.innerHTML = '';
            
            if (rooms && rooms.length > 0) {
                rooms.forEach(room => {
                    const row = document.createElement('tr');
                    
                    // 处理设施列表
                    let facilitiesHtml = '';
                    if (room.facilities) {
                        const facilities = room.facilities.split(',');
                        facilitiesHtml = '<div class="facilities-list">';
                        facilities.forEach(facility => {
                            facilitiesHtml += `<span class="facility-tag">${facility.trim()}</span>`;
                        });
                        facilitiesHtml += '</div>';
                    } else {
                        facilitiesHtml = '-';
                    }
                    
                    // 处理状态显示
                    const statusBadge = room.status === 'free' 
                        ? '<span class="status-badge status-free">空闲</span>' 
                        : '<span class="status-badge status-occupied">占用</span>';
                    
                    // 处理会议信息
                    let meetingsHtml = '';
                    if (room.currentMeetings && room.currentMeetings.length > 0) {
                        meetingsHtml = '<div class="meeting-info">';
                        room.currentMeetings.forEach(meeting => {
                            let startTime = '-';
                            let endTime = '-';
                            
                            if (meeting.startTime) {
                                // 解析后端返回的时间字符串
                                const startDateTime = new Date(meeting.startTime);
                                if (!isNaN(startDateTime.getTime())) {
                                    startTime = startDateTime.toLocaleString('zh-CN', {
                                        year: 'numeric',
                                        month: '2-digit',
                                        day: '2-digit',
                                        hour: '2-digit',
                                        minute: '2-digit',
                                        second: '2-digit'
                                    });
                                }
                            }
                            
                            if (meeting.endTime) {
                                // 解析后端返回的时间字符串
                                const endDateTime = new Date(meeting.endTime);
                                if (!isNaN(endDateTime.getTime())) {
                                    endTime = endDateTime.toLocaleString('zh-CN', {
                                        year: 'numeric',
                                        month: '2-digit',
                                        day: '2-digit',
                                        hour: '2-digit',
                                        minute: '2-digit',
                                        second: '2-digit'
                                    });
                                }
                            }
                            
                            meetingsHtml += `
                                <div class="meeting-item">
                                    <div class="meeting-topic">${meeting.meetingTopic || '未命名会议'}</div>
                                    <div class="meeting-time">${startTime} - ${endTime}</div>
                                    <div>预订人: ${meeting.bookerName || '-'}</div>
                                </div>
                            `;
                        });
                        meetingsHtml += '</div>';
                    } else {
                        meetingsHtml = '-';
                    }
                    
                    row.innerHTML = `
                        <td>${room.roomId || '-'}</td>
                        <td>${room.roomName || '-'}</td>
                        <td>${facilitiesHtml}</td>
                        <td>${statusBadge}</td>
                        <td>${meetingsHtml}</td>
                    `;
                    tbody.appendChild(row);
                });
            } else {
                const row = document.createElement('tr');
                row.innerHTML = `<td colspan="5" class="no-results">未找到符合条件的会议室</td>`;
                tbody.appendChild(row);
            }
        }
        
        // 重置表单
        function resetForm() {
            // 重置时间为默认值（当前时间到一小时后）
            const now = new Date();
            const oneHourLater = new Date(now.getTime() + 60 * 60 * 1000);
            
            const formatDateTime = (date) => {
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                const hours = String(date.getHours()).padStart(2, '0');
                const minutes = String(date.getMinutes()).padStart(2, '0');
                return `${year}-${month}-${day}T${hours}:${minutes}`;
            };
            
            document.getElementById('startTime').value = formatDateTime(now);
            document.getElementById('endTime').value = formatDateTime(oneHourLater);
            document.getElementById('resultsTableBody').innerHTML = '<tr><td colspan="5" class="no-results">请输入查询条件并点击查询按钮</td></tr>';
        }
        
        // 显示通知
        function showNotification(message, type) {
            const notification = document.getElementById('notification');
            notification.textContent = message;
            notification.className = 'notification ' + type;
            notification.style.display = 'block';
            
            setTimeout(() => {
                notification.style.display = 'none';
            }, 3000);
        }
    </script>
</body>
</html>